﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Net;
using System.ComponentModel;
using System.IO;

namespace com.ubosi.spider.config
{
    [XmlType("spider.configuration")]
    public class SystemConfig
    {
        [XmlElement("connectionString")]
        public string ConnectionString { get; set; }

        private NetWorkConfig _networkConfig = null;
        [XmlElement("webRequest")]
        public NetWorkConfig NetWorkConfig
        {
            get
            {
                if (_networkConfig == null)
                    _networkConfig = new NetWorkConfig();
                return _networkConfig;
            }
            set
            {
                _networkConfig = value;
            }
        }

        private SpiderSchedulerConfig _spiderSchedulerConfig = null;
        [XmlElement("spiderScheduler")]
        public SpiderSchedulerConfig SpiderSchedulerConfig 
        {
            get
            {
                if (_spiderSchedulerConfig == null)
                    _spiderSchedulerConfig = new SpiderSchedulerConfig();
                return _spiderSchedulerConfig;
            }
            set
            {
                _spiderSchedulerConfig = value;
            }
        }

        private AnalyzerSchedulerConfig _analyzerSchedulerConfig = null;
        [XmlElement("analyzerScheduler")]
        public AnalyzerSchedulerConfig AnalyzerSchedulerConfig
        {
            get
            {
                if (_analyzerSchedulerConfig == null)
                    _analyzerSchedulerConfig = new AnalyzerSchedulerConfig();
                return _analyzerSchedulerConfig;
            }
            set
            {
                _analyzerSchedulerConfig = value;
            }
        }
        private ExporterSchedulerConfig _exorterSchedulerConfig = null;
        [XmlElement("exporterScheduler")]
        public ExporterSchedulerConfig ExporterSchedulerConfig
        {
            get
            {
                if (_exorterSchedulerConfig == null)
                    _exorterSchedulerConfig = new ExporterSchedulerConfig();
                return _exorterSchedulerConfig;
            }
            set
            {
                _exorterSchedulerConfig = value;
            }
        }
    }

    /// <summary>
    /// 分析器配置
    /// </summary>
    [XmlType("analyzerScheduler")]
    public class AnalyzerSchedulerConfig
    {
        private int _maxConcurrent = 5;
        [XmlAttribute("maxConcurrent")]
        public int MaxConcurrent
        {
            get
            {
                return _maxConcurrent;
            }
            set
            {
                _maxConcurrent = value;
            }
        }

 
        private string _analyzerPath = string.Empty;
        [XmlAttribute("analyzerPath")]
        public string AnalyzerPath
        {
            get
            {
                return _analyzerPath;
            }
            set
            {
                _analyzerPath = value;
            }
        }
        private int _eachInterval = 100;
        [XmlAttribute("eachInterval")]
        public int EachInterval
        {
            get
            {
                return _eachInterval;
            }
            set
            {
                _eachInterval = value;
            }
        }
        private int _batchCount = 200;
        [XmlAttribute("batchCount")]
        public int BatchCount
        {
            get
            {
                return _batchCount;
            }
            set
            {
                _batchCount = value;
            }
        }
        private int _batchInterval = 10000;
        [XmlAttribute("batchInterval")]
        public int BatchInterval
        {
            get
            {
                return _batchInterval;
            }
            set
            {
                _batchInterval = value;
            }
        }
    }

    /// <summary>
    /// 抓取调度器配置
    /// </summary>
    [XmlType("spiderScheduler")]
    public class SpiderSchedulerConfig
    {
        private int _maxConcurrent = 5;
        [XmlAttribute("maxConcurrent")]
        public int MaxConcurrent
        {
            get
            {
                return _maxConcurrent;
            }
            set
            {
                _maxConcurrent = value;
            }
        }


        private int _eachInterval = 100;
        [XmlAttribute("eachInterval")]
        public int EachInterval
        {
            get
            {
                return _eachInterval;
            }
            set
            {
                _eachInterval = value;
            }
        }
        private int _batchCount = 200;
        [XmlAttribute("batchCount")]
        public int BatchCount
        {
            get
            {
                return _batchCount;
            }
            set
            {
                _batchCount = value;
            }
        }
        private int _batchInterval = 10000;
        [XmlAttribute("batchInterval")]
        public int BatchInterval
        {
            get
            {
                return _batchInterval;
            }
            set
            {
                _batchInterval = value;
            }
        }
    }

    /// <summary>
    /// 抓取调度器配置
    /// </summary>
    [XmlType("exporterScheduler")]
    public class ExporterSchedulerConfig
    {
        private int _maxConcurrent = 5;
        [XmlAttribute("maxConcurrent")]
        public int MaxConcurrent
        {
            get
            {
                return _maxConcurrent;
            }
            set
            {
                _maxConcurrent = value;
            }
        }


        private int _eachInterval = 100;
        [XmlAttribute("eachInterval")]
        public int EachInterval
        {
            get
            {
                return _eachInterval;
            }
            set
            {
                _eachInterval = value;
            }
        }
        private int _batchCount = 200;
        [XmlAttribute("batchCount")]
        public int BatchCount
        {
            get
            {
                return _batchCount;
            }
            set
            {
                _batchCount = value;
            }
        }
        private int _batchInterval = 10000;
        [XmlAttribute("batchInterval")]
        public int BatchInterval
        {
            get
            {
                return _batchInterval;
            }
            set
            {
                _batchInterval = value;
            }
        }
    }
    /// <summary>
    /// 网络访问器配置信息
    /// 主要配置向外发出的请求模拟的一些http信息
    /// </summary>
    [XmlType("webRequest")]
    public class NetWorkConfig
    {
        private string _userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4";
        private string _accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
       [XmlElement("userAgent")]
        public string UserAgent
        {
            get{
                return _userAgent;
            }
            set{
                _userAgent = value;
            }
        }
        [XmlElement("accept")]
        public string Accept
        {
            get
            {
                return _accept;
            }
            set
            {
                _accept = value;
            }
        }
        private int _proxyTimeout = 5;
        [XmlAttribute("proxyTimeout")]
        public int ProxyTimeout { get { return _proxyTimeout; } set { _proxyTimeout = value; } }
        private int _timeout = 60;
        [XmlAttribute("timeout")]
        public int Timeout { get { return _timeout; } set { _timeout = value; } }
        [XmlAttribute("useProxy")]
        public bool UseProxy { get; set; }
        private DecompressionMethods _decompressionMethods = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        [XmlAttribute("decompressionMethods")]
        public DecompressionMethods DecompressionMethods
        {
            get
            {
                return _decompressionMethods;
            }
            set
            {
                _decompressionMethods = value;
            }
        }

        private bool _allowAutoRedirect = true;
        [XmlAttribute("allowAutoRedirect")]
        public bool AllowAutoRedirect
        {
            get
            {
                return _allowAutoRedirect;
            }
            set
            {
                _allowAutoRedirect = value;
            }
        }
        [XmlAttribute("referer")]
        public string Referer { get; set; }
        private List<Header> _headers;
        [XmlArray("headers")]
        [XmlArrayItem("header")]
        public List<Header> Headers
        {
            get
            {
                if (_headers == null)
                    _headers = new List<Header>();
                return _headers;
            }
        }
        private int _tryCount = 1;
        /// <summary>
        /// 尝试次数
        /// </summary>
        [XmlElement("trycount")]
        public int TryCount
        {
            get
            {
                return _tryCount;
            }
            set
            {
                _tryCount = value;
            }
        }
        private List<Proxy> _proxys;
        [XmlArray("proxys")]
        [XmlArrayItem("proxy")]
        public List<Proxy> Proxys
        {
            get
            {
                if (_proxys == null)
                    _proxys = new List<Proxy>();
                return _proxys;
            }
        }
    }
    [XmlType("header")]
    public class Header
    {
        [XmlAttribute("name")]
        public HttpRequestHeader Name { get; set; }
        [XmlAttribute("value")]
        public string Value { get; set; }

        public Header()
        {
        }
        public Header(HttpRequestHeader name, string value)
        {
            this.Name = name;
            this.Value = value;
        }
    }
    [XmlType("proxy")]
    public class Proxy
    {
        [XmlAttribute("host")]
        public string Host { get; set; }
        [XmlAttribute("port")]
        public int Port { get; set; }
        [XmlAttribute("domain")]
        public string Domain { get; set; }
        [XmlAttribute("user")]
        public string User { get; set; }
        [XmlAttribute("pwd")]
        public string Password { get; set; }
    }
}
